#!/bin/bash

DIR="$(dirname "$0")"
VARGUID="FFF12B8D-7696-4C8B-A985-2747075B4F50"
NVARGUID="CEF5B9A3-476D-497F-9FDC-E98143E0422C"
if (( $# <= 1 )) ; then
  echo "version: V1.5.28"
  echo "Copyright (c) 2005-2023, $COPY_RIGHT"
  echo "USAGE: sudo ./SetupTool export(import default ShowCfg modify)"
  echo "export:  Export configuration to config file"
  echo "import:  Import the contents of the config file into the configuration"
  echo "default: Clear the entire variable area and restore the default variables"
  echo "ShowCfg: Display all configuration information"
  echo "modify: Modify a certain configuration information, such as./SetupTool modify \"Fast Boot\" 1" 
  version=$($DIR/flashrom -h | grep "flashrom V" | awk -F"\r" '{print $1}')
  echo $version
  exit 1;
fi

space=" "
cmd=$*
plat=""
chip=0
setupcmd=""
countparam=0
firstBoot=0

flashrom_cmd=""
FlashName=""

plat1="D2000/8"
plat2="FT-2500"
plat3="Hygon"
plat4="Loongson-3A"
plat5="SW"
plat6="FT-2000/4"
plat7="FT2000PLUS"
plat8="3C5000L"
plat9="3A5000"
plat10="3C5000"
plat11="ZHAOXIN"
plat12="S5000C"
plat13="3A6000"
plat14="Intel"
plat15="FT-D3000"
plat16="FT-E2000"
plat_all=$plat1$space$plat2$space$plat3$space$plat4$space$plat5$space$plat6$space$plat7$space$plat8$space$plat9$space$plat10$space$plat11$space$plat12$space$plat13$space$plat14$space$plat15$space$plat16

function switchPlat() {
  sPlat=""
  if [[ $1 =~ $plat1 ]] ; then
    sPlat="FT2000"
  elif [[ $1 =~ $plat2 ]] ; then
    sPlat="FT2500"
  elif [[ $1 =~ $plat3 ]] ; then
    sPlat="internal"
  elif [[ $1 =~ $plat11 ]] ; then
    sPlat="internal"
  elif [[ $1 =~ $plat14 ]] ; then
    sPlat="internal"
  elif [[ $1 =~ $plat4 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat5 ]] ; then
    sPlat="SW64"
  elif [[ $1 =~ $plat6 ]] ; then
    sPlat="FT2000"
  elif [[ $1 =~ $plat7 ]] ; then
    sPlat="FT2000PLUS"
  elif [[ $1 =~ $plat8 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat9 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat10 ]] ; then
    sPlat="LS3C5000"
  elif [[ $1 =~ $plat12 ]] ; then
    sPlat="FT5000"
  elif [[ $1 =~ $plat15 ]] ; then
    sPlat="FTD3000"
  elif [[ $1 =~ $plat13 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat16 ]] ; then
    sPlat="FTE2000"
  fi
  echo $sPlat
}

function getCmdPlat() {
  sPlat=""
  for param in $*
  do
    if [[ $param =~ "--plat=" ]] ; then
      sPlat=$(switchPlat $param)
    else
      sPlat=$sPlat
    fi
  done
  echo $sPlat
}

function catCpuInfo() {
	echo `cat /proc/cpuinfo | grep "model name" | awk -F":" '{print $2}'| awk 'NR==1'`
}

function getCpuInfoPlat() {
  sPlat=""
  sPlat=$(catCpuInfo)
  sPlat=$(switchPlat "$sPlat")
  echo $sPlat
}

function getPlat() {
  sPlat=""
  if [[ $* =~ "--plat=" ]] ; then
    sPlat=$(getCmdPlat $*)
  else 
    sPlat=$(getCpuInfoPlat)
  fi
  echo $sPlat
}

#echo "cmd: "$cmd
plat=$(getPlat $cmd)
#echo "plat ==== "$plat
if [ ! $plat ];then
  echo "plat is NULL,Please try adding --plat=" $plat_all "etc."
#  echo "Unknown or unsupport platform!"
  exit 1;
fi

chmod 777 $DIR/*

for param in $*
do
  if ((countparam > 0));then
    if test $param = "-c" ;then
      chip=1
      flashrom_cmd=$param
    elif test $chip = 1 ;then
      flashrom_cmd=$flashrom_cmd$space$param
      chip=0
    elif test $param = "SetManuFirBoot" ;then
      setupcmd=$param
      firstBoot=1
    elif test $param = "SetPrimaryFirBoot" ;then
      setupcmd=$param
      firstBoot=1
    elif test $firstBoot = 1 ;then
      setupcmd=$setupcmd$space$param
      firstBoot=0
    elif test $param = "export" -o $param = "import"  -o $param = "default" -o $param = "BootEx" -o $param = "InitCfg" -o $param = "ShowCfg"  -o $param = "ManuBootDef" -o $param = "PrimaryBootDef" ;then
      setupcmd=$param
    elif [[ "$param" == "FixVar"* ]]; then
      setupcmd=$param
    elif [[ "$param" == "--singleboot="* ]]; then
     # echo  $param
      setupcmd=$setupcmd$space$param
    elif [[ "$param" == "--plat="* ]]; then
      echo ""
    else
      echo "$param is illegal"
      exit 1
    fi
  fi
  countparam=$countparam+1
done

if test -z "$setupcmd" ;then
  echo "version: V1.5.28"
  echo "Copyright (c) 2005-2023, $COPY_RIGHT"
  echo "USAGE: sudo ./SetupTool export(import default ShowCfg modify)"
  echo "export:  Export configuration to config file"
  echo "import:  Import the contents of the config file into the configuration"
  echo "default: Clear the entire variable area and restore the default variables"
  echo "ShowCfg: Display all configuration information"
  echo "modify: Modify a certain configuration information, such as./SetupTool modify \"Fast Boot\" 1" 
  version=$($DIR/flashrom -h | grep "flashrom V" | awk -F"\r" '{print $1}')
  echo $version
  exit 1;
fi

if [[ $setupcmd =~ "import" ]] ; then
  if [ ! -f "./config/config" ]; then
    echo -e "\033[41;5mUnable to find the configuration file, Please use the export command first.\033[0m"
    exit 1
  fi
fi

$DIR/flashrom $flashrom_cmd -p $plat -r $1

rm -rf $1-unpack*
$DIR/UEFIExtract $1 unpack > /dev/null

rm -rf $DIR/ext
mkdir $DIR/ext
cp -r $1-unpack.dump/*PE32*.bin $DIR/ext
cp -r $1-unpack.dump/Section_Raw_*.bin  $DIR/ext  

if [ -f "$1-unpack.dump/File_Raw_${NVARGUID}_NVAR_store_info.txt" ]; then
  cp -r $1-unpack.dump/*$NVARGUID*.bin $DIR/ext
  rm -rf ./var
  mkdir ./var
  cp -r $1-unpack.dump/*$NVARGUID*.txt ./var
else
  cp -r $1-unpack.dump/*${VARGUID}*.bin $DIR/ext
  rm -rf ./var
  mkdir ./var
  cp -r $1-unpack.dump/*${VARGUID}*.txt ./var
fi
rm -rf $1-unpack*

FlashName=$(bash $DIR/setuptool_layout.sh)
if [ ! -f "$DIR/Layout" ];then
  echo -e "\033[41;5mERR: File Layout not found!!!\033[0m"
  exit 1;
fi

if [ ! -f $1 ];then
  echo "ERR: File $1 not found!!!"
  exit 1;
fi
#echo $setupcmd  
$DIR/ifrextract $DIR/ext ./config $1 $setupcmd
if [ $? -ne 0 ]; then
    echo -e "\033[41;5m$setupcmd fail\033[0m"
    exit 1
fi

if test "$setupcmd" !=  "export" -a "$setupcmd" != "BootEx" -a "$setupcmd" != "InitCfg" -a "$setupcmd" != "ShowCfg"; then
  #echo $DIR/flashrom $flashrom_cmd -p $plat -l $DIR/Layout -N -i NVFV -w out.bin
  $DIR/flashrom $flashrom_cmd -p $plat -l $DIR/Layout -N $FlashName -w out.bin
  rm $DIR/Layout
fi

rm -rf $DIR/ext
rm -rf ./var
#rm -rf *.bin
exit $?